/**
 保存流程时的弹框
 */
<template>
  <div class="">
    <el-form ref="flowForm" :model="flowForm" label-width="80px">
      <el-form-item label="名称" :rules="rules.name" prop="name">
        <el-input v-model="flowForm.name"></el-input>
      </el-form-item>
      <el-form-item label="路径" :rules="rules.path" prop="path">
        <el-input v-model="flowForm.path"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import API from '../../api/index'

export default {
  name: 'SaveFlow',
  props: {
  },
  data () {
    return {
      defaultProps: {
        children: 'children',
        label: 'name',
        isLeaf: 'leaf'
      },
      flowForm: {
        name: '',
        path: '',
        realPath: '',
        validateFlag: false
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        path: [
          { required: true, message: '请选择路径', trigger: 'blur' }
        ]
      },
      node: [],
      resolve: []
    }
  },
  computed: {
    userName: {
      get () { return this.$store.state.user.name }
    }
  },
  methods: {
    // 获取数据=======================
    // 获取目录、转换及作业列表
    listRepository (path) {
      var params = {
        'path': path
      }
      return API.repository.listRepository(params)
    },
    // 异步加载树节点数据
    loadNode (node, resolve) {
      this.node = node
      this.resolve = resolve
      if (!this.userName) {
        this.$notify.error({
          title: '错误',
          message: '未取到用户名，请联系管理员',
          duration: 0
        })
        return
      }
      if (node.level === 0) {
        this.listRepository('/').then(res => {
          if (res.data && res.data.length > 0) {
            return resolve(res.data)
          } else {
            return resolve([{ 'name': '/', path: '/', realPath: '/' + this.userName, leaf: true }])
          }
        })
      } else {
        this.listRepository(node.data.path).then(res => {
          return resolve(res.data)
        })
      }
    },

    getPathOptions () {
      this.node.childNodes = []
      this.loadNode(this.node, this.resolve)
    },
    // 选中节点
    nodeClick (data, node) {
      this.flowForm.path = data.path
      this.flowForm.realPath = data.realPath
    },
    // 清除选择
    clearHandle () {
      this.flowForm.path = ''
    },
    // 表单验证
    validateForm () {
      this.flowForm.validateFlag = false
      this.$refs['flowForm'].validate((valid) => {
        if (valid) {
          this.flowForm.validateFlag = true
        }
      })
    },
    // 重置
    reset () {
      this.flowForm.path = ''
      this.flowForm.name = ''
      this.flowForm.realPath = ''
    }
  },
  mounted () {
  }
}
</script>

